const Storage = require('./storage.js');


const DOCUMENT = 'global_config_auth_info';

class GlobalConfig {
  constructor(workspaceId) {
    this.workspaceId = workspaceId;
    this.storage = new Storage(workspaceId);
  }

  async saveAuthInfo(authData) {
    const curAuthInfo = await this.getAuthInfo();
    if (!curAuthInfo) {
      return await this.storage.save(DOCUMENT, authData);
    }
    // 存在则更新
    return await this.updateAuthInfo(authData);
  }

  async updateAuthInfo(authData) {
    return await this.storage.update(DOCUMENT, {}, authData);
  }

  async getAuthInfo() {
    const result = await this.storage.query(DOCUMENT, {});
    if (result.length > 0) {
      return result[0];
    }

    console.log('getAuthInfo return null', result[0], result.length);
    return null;
  }
}

module.exports = GlobalConfig;
